package com.nf.health.app.myfamily.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.utils.ViewUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 此demo用来展示如何结合定位SDK实现定位，并使用MyLocationOverlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡
 * 
 */
public class OldPersonalMapActivity extends BaseActivity {



	// 定位相关
	LocationClient mLocClient;
	//	public MyLocationListenner myListener = new MyLocationListenner();
	BitmapDescriptor mCurrentMarker;

	MapView mMapView;
	BaiduMap mBaiduMap;

	List<LatLng> points = new ArrayList<LatLng>();//坐标集合

	boolean isFirstLoc = true;// 是否首次定位

	private TextView old_map_mi,old_map_content,old_map_title;


	String[] ars;

	String address,radius,address1,radius1;//地址


	public MyLocationListenner myListener = new MyLocationListenner();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_old_personal_map);

		ViewUtil.setText(this, R.id.titlebar_title_tv, "轨迹");

		old_map_mi = (TextView) findViewById(R.id.old_map_mi);
		old_map_content = (TextView) findViewById(R.id.old_map_content);
		old_map_title = (TextView) findViewById(R.id.old_map_title);



		String imei = getIntent().getStringExtra("imei");

		// 地图初始化
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

		provider.Get_Old_Map(imei,"Old_Map");			




		// 开启定位图层
		mBaiduMap.setMyLocationEnabled(true);
		// 定位初始化
		mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(1000);

		mLocClient.setLocOption(option);
		mLocClient.start();


	}



	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// 退出时销毁定位
		//		mLocClient.stop();
		// 关闭定位图层
		mBaiduMap.setMyLocationEnabled(false);
		mMapView.onDestroy();
		mMapView = null;
		super.onDestroy();
	}
	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);


		if("Old_Map".equals(actionName)){


			try {

				if(object.equals("[]")){
					showToast("未获取到轨迹数据");
				}else{
					JSONArray jsonArray = new JSONArray(object+"");
					for(int i = 0;i < jsonArray.length();i++){

						String jsonarray = jsonArray.getString(i);

						JSONObject jsonObject2 = new JSONObject(jsonarray);

						String lon = jsonObject2.getString("lon");
						String lat = jsonObject2.getString("lat");
						address = jsonObject2.getString("address");
						radius = jsonObject2.getString("radius");

						if(address.equals("")){

						}else{
							address1 =address;
						}
						if(radius.equals("")){

						}else{
							radius1 = radius;
						}
						if(lon.equals("") || lat.equals("")){

						}else{
							points.add(new LatLng(Double.parseDouble(lat),Double.parseDouble(lon)));
						}
					}
					weizhi();
				}


			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


		}


	}
	private void weizhi() {
		// TODO Auto-generated method stub
		if(TextUtils.isEmpty(address1)){

			showToast("未获取位置名称");

		}else{
			ars = address1.split(";");


			String arstitle = ars[0];
			String arscontent =	ars[1];

			old_map_mi.setText(radius1+"米");

			old_map_title.setText(arstitle);

			old_map_content.setText(arscontent);
		}



		LatLng point1 = points.get(0);

		LatLng point = point1;  //起点图标
		//构建Marker图标  
		BitmapDescriptor bitmap = BitmapDescriptorFactory  
				.fromResource(R.drawable.icon_goto);  
		//构建MarkerOption，用于在地图上添加Marker  
		OverlayOptions option = new MarkerOptions()  
		.position(point)  
		.icon(bitmap);  
		//在地图上添加Marker，并显示  
		mBaiduMap.addOverlay(option);
		LatLng point3 = points.get(points.size()-1);
		LatLng point2 = point3;  //终点图标
		//构建Marker图标  
		BitmapDescriptor bitmap2 = BitmapDescriptorFactory  
				.fromResource(R.drawable.zhongdian_icon);  
		//构建MarkerOption，用于在地图上添加Marker  
		OverlayOptions option2 = new MarkerOptions()  
		.position(point2)  
		.icon(bitmap2);  
		//在地图上添加Marker，并显示  
		mBaiduMap.addOverlay(option2);




		OverlayOptions ooPolyline = new PolylineOptions().width(10)
				.points(points).color(R.drawable.line);
		//		Polyline  mPolyline = (Polyline) mBaiduMap.addOverlay(ooPolyline);

		mBaiduMap.addOverlay(ooPolyline);
	}
	/**
	 * 定位SDK监听函数
	 */
	public class MyLocationListenner implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			// map view 销毁后不在处理新接收的位置
			if (location == null || mMapView == null)
				return;
			if (isFirstLoc) {
				isFirstLoc = false;

				if(points.size() == 0){

				}else{
					LatLng point1 = points.get(0);

					LatLng ll = point1;

					float f = mBaiduMap.getMaxZoomLevel();//19.0
					float m = mBaiduMap.getMinZoomLevel();//3.0
					MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, f-2);

					mBaiduMap.animateMapStatus(u);
				}
			}
		}

		public void onReceivePoi(BDLocation poiLocation) {
		}
	}

}
